package controller;

import Service.UserService;
import com.opensymphony.xwork2.ActionSupport;
import entity.Users;
import org.apache.struts2.ServletActionContext;

public class UserLoginAction extends ActionSupport {
    private String name;
    private String password;
    private String errorStr;

    @Override
    public String execute() throws Exception {
        if (name==null||password==null){
            errorStr="账号和密码不能为空";
            ServletActionContext.getRequest().setAttribute("errorStr",errorStr);
            return "error";
        }
        if (name.equals("")||password.equals("")){
            errorStr="账号和密码不能为空";
            ServletActionContext.getRequest().setAttribute("errorStr",errorStr);
            return "error";
        }
         else {
            Users users = new Users(name,password);
            UserService userService = new UserService();
            Users users1 = userService.login(users);
            if (users1!=null){
                ServletActionContext.getRequest().setAttribute("user",users);
                return SUCCESS;
            }else {
                errorStr="账号或者密码错误";
                ServletActionContext.getRequest().setAttribute("errorStr",errorStr);
                return "error";
            }

            }
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }
}
